#ifndef _THREAD_HPP_
#define _THREAD_HPP_
#include <pthread.h>
#include <stdio.h>
#include "Uncopyable.hpp" //TODO: must be delete when use in framework
#include "Mutex.hpp"
#include "ThreadManager.hpp"
#include "ThreadJob.hpp"

//Future implementation: function restart() (use the same function/args) and call start

//LIMITATION:	The "Stop function" cannot totally work, it unblock a blocking call, but kill fonction in win32 pthread
//				isn't totally implemented yet
//RISK:			Unlock the Mutex while a pthread is still running

class Thread : public Uncopyable
{
public:
	Thread(void);
	~Thread(void);

public:
	bool Start(ThreadJob * Job);
	bool Start(); //Start the saved job
	bool Restart(); //Restart a job  :: Start() alias
	bool Stop(void);
	bool Join(void);
	pthread_t GetId(void);
	int GetState(void);
	ThreadManager * getManager(void);
	bool IsThreadEqual(pthread_t t);
	bool IsThreadEqual(pthread_t t1, pthread_t t2);
	void SetThreadJob(ThreadJob * Job);
	

private:
	pthread_t m_iId;
	//void * m_pvThreadArgs;	
	Mutex m_startMutex; 	
	static ThreadManager m_tm;
	static void * ThreadFunc(void *Arg); //thread helper function
	ThreadJob * m_job;
	bool JobIsSet;
};

#endif // _THREAD_HPP_


/*
http://www.thescripts.com/forum/thread128659.html

Problems are:
1. A destructor absolutely must call pthread_join or the lifetime of the
thread is longer than that of the object - this in turn means that the
thread cannot be detached and also that a call to the destructor might never
return.

2. It is possible for the thread to start, do its job and exit before the
creating thread even returns from pthread_create i.e. you may well need a
condition to be signalled from the thread to the ctor to indicate that it is
realy going.
*/
